// pages/subpackage/DetailsOfTheTrip/index.js
// 这里是不带地图的详情页 （行程页）
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: 20,
    answerSlideshow: {
        pages: [],
        total: 0,
        currentId: 0
      },
    ScheduleOrder: [],
    id: '',
    TravelInformation: {},
    phone: wx.getStorageSync('TwoPlusTravel').phone || '',
    role: 0,
    shareObj: {},
    swiperNum: 0,
    PicturesShow: false,
    
    previewShow: false,
    previewimg: 'https://cos.erjiakj.cn//share/mban1.png',
    intervalId: null,
    show: false,
    columns: [],
    PreSeat: 0,
    NewRelease: false,
    ShareCardShow: false,
    Sharing: false,

    passengerOrDriverRole: null,
    isPay: null,
    detailsUpdateBy: null,
    goodsUpdateBy: null,
    detailsId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("=========================进入DetailsOfTheTrip=====================");
    // debugger
    this.setData({
      id: options.id,
      role: Number(options.role),
      StatusBar: app.globalData.StatusBar,
      passengerOrDriverRole: options.role,
      isPay: options.isPay,
      detailsUpdateBy: options.detailsUpdateBy,
      goodsUpdateBy: options.goodsUpdateBy,
      detailsId: options.detailsId
    })
    if (options.Showshare) {
      this.setData({
        NewRelease: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.Sharing) {
      this.setData({
        ShareCardShow: false
      })
    }
    this.ResettheAcquisition()
    this.getDetailsshare()
    this.getCarDetailsTriAjax()
  },
  // 授权获取地理位置
  getLocation() {
    let that = this
    wx.getLocation({
      type: 'gcj02', //表示使用GPS定位
      success: (res) => {
        app.globalData.position.latitude = res.latitude;
        app.globalData.position.longitude = res.longitude;
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude
        })
      },
      //表示用户没有开启授权
      fail: (res) => {
        wx.showToast({
          title: '已取消定位,授权定位服务',
          icon: 'none'
        })
        let timer = setTimeout(() => {
          wx.showModal({
            title: '',
            confirmColor: '#b4282d',
            content: '需要获取地理位置 请确认授权',
            success: function (res) {
              if (res.confirm) {
                wx.openSetting({
                  success(res) {
                    if (!res.authSetting['scope.userLocation']) {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'error'
                      })
                    }
                  },
                  fail(res) {
                    wx.showToast({
                      title: '授权失败',
                      icon: 'error'
                    })
                  }
                })
              }
            }
          })
          clearTimeout(timer)
        }, 1000)
      }
    })
  },
  //   轮询获取实时数据
  getCarDetailsTriAjax() {
    let that = this
    util.request(
      'GET',
      api.CarDetailsTriAjax, {
        id: this.data.id,
        phone: this.data.phone,
        passengerOrDriverRole: this.data.passengerOrDriverRole,
        state: "Trip"
      }
    ).then(function (res) {
      // debugger
      if (that.data.passengerOrDriverRole == 1) { //车主
        that.setData({
          PriceData: res.PriceData,
          ScheduleOrder: res.data
        })
      } else if (that.data.passengerOrDriverRole == 2) { //乘客
        // debugger
        if (!res.userId) {
          wx.showToast({
            title: '车主取消接单,等待再次接单',
            icon: 'none'
          })
          let time = setTimeout(() => {
            wx.reLaunch({
              url: '/pages/subpackage/DetailsPassengerTravel/index?id=' + that.data.id + '&detailsId=' + that.data.detailsId,
            })
            clearTimeout(time)
          }, 800)
          return
        }

        if (res.update_by == 4) {
          wx.showToast({
            title: '行程结束',
            icon: 'none'
          })
          let time = setTimeout(() => {
            wx.redirectTo({
              url: '/pages/index/index'
            })
            clearTimeout(time)
          }, 800)
          return
        }

        if (res.update_by == 0 && res.userId) {
          wx.showToast({
            title: '行程已取消',
            icon: 'none'
          })
          let time = setTimeout(() => {
            wx.redirectTo({
              url: '/pages/index/index'
            })
            clearTimeout(time)
          }, 800)
          return
        }

        that.setData({
          'TravelInformation.class': res.class,
          'TravelInformation.details_by': res.details_by,
          'TravelInformation.role': res.role,
          'TravelInformation.update_by': res.update_by
        })
        // debugger
        // const originalData = that.data.TravelInformation;
        // Object.assign({}, originalData, {
        //     details_by: res.details_by,
        //     role: res.role,
        //     update_by: res.update_by,
        //     class: res.class
        // })
      }
    })
  },
  // 获取详情
  getDetailsTrip() {
    //   debugger
    let that = this
    util.request(
      'GET',
      api.DetailsTrip, {
        id: this.data.id,
        phone: this.data.phone,
        passengerOrDriverRole: this.data.passengerOrDriverRole,
        // isPay: this.data.isPay,
        // detailsUpdateBy: this.data.detailsUpdateBy,
        // goodsUpdateBy: this.data.goodsUpdateBy,
        // detailsId: this.detailsId
        state: "Trip"
      }
    ).then(function (res) {
      if (res.update_by < 1) {
        clearInterval(that.data.intervalId);
        that.setData({
          intervalId: null
        })
        wx.showToast({
          title: '行程不存在',
          duration: 1000
        })
        let time = setTimeout(() => {
          wx.redirectTo({
            url: '/pages/index/index',
          })
          clearTimeout(time)
        }, 1000)
        return
      }
      if (res) {
        let data = res
        data.goOff = res.godate
        data.godate = util.timeChangeover(data.godate)
        if (that.data.role == 1) {
          that.setData({
            ScheduleOrder: data.data
          })
        }
        that.setData({
          TravelInformation: data,
          PreSeat: data.seat,
          ScheduleOrder: res.Details
        })
        wx.stopPullDownRefresh()
      }
    })
  },
  // 获取分享
  getDetailsshare() {
    let that = this
    util.request('GET', api.getDetailsshare, {
      id: this.data.id
    }).then(function (res) {
      console.log(res);
      if (Object.keys(res).length) {
        that.setData({
          shareObj: res
        })
        if (that.data.NewRelease && !that.data.Sharing) {
          that.setData({
            ShareCardShow: true
          })
        }
      }
    })
  },
  MyScheduleOrder() {
    clearInterval(this.data.intervalId)
    this.setData({
      intervalId: null
    })
    // 获取页面栈
    const pages = getCurrentPages();
    // 上一层页面是页面栈的倒数第二个
    const prevPage = pages[pages.length - 2];
    // 如果上一层页面存在，则可以返回
    if (prevPage && prevPage.route != 'pages/subpackage/TheTripInitiated/index' && prevPage.route != 'pages/subpackage/DetailsPassengerTravel/index') {
      wx.navigateBack();
    } else {
      // 没有上一层页面的处理逻辑
      wx.reLaunch({
        url: '/pages/index/index'
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.data.intervalId);

    this.setData({
      intervalId: null
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    let time = setTimeout(() => {
      this.ResettheAcquisition()
      clearTimeout(time)
    }, 800)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let shareObj = this.data.shareObj
    var title = `车找人 ${shareObj.setAddress}-${shareObj.reachAddress}` // 标题
    let id = this.data.id
    return {
      title: title,
      imageUrl: shareObj.image_url,
      path: `/pages/subpackage/TheTripInitiated/index?id=${id}&isShare=1`
    }
  },
  // 调起手机拨号
  dialANumber(e) {
    let num = e.currentTarget.dataset.num
    wx.makePhoneCall({
      phoneNumber: num
    })
  },
  // 行程取消
  TheTripCancelled() {
    let role = this.data.role
    if (role == 1) {
      if (this.data.TravelInformation.update_by > 2 && this.data.TravelInformation.details_by != 2) {
        let tit = this.data.TravelInformation.update_by == 4 ? '行程已结束' : '行程已出发 不可中途取消'
        wx.showToast({
          title: tit,
          icon: 'none'
        })
        return
      }
    } else {
      if (this.data.TravelInformation.details_by != 1 && this.data.TravelInformation.details_by != 2) {
        let tit = this.data.TravelInformation.update_by != 1 && this.data.TravelInformation.details_by != 2 ? '行程已出发 不可中途取消' : '行程已结束'
        wx.showToast({
          title: tit,
          icon: 'none'
        })
        return
      }
    }
    let that = this
    let Tiptitle = ''
    let isWithinOneHour = util.isWithinOneHour(this.data.TravelInformation.goOff)
    if (role == 1) {
      Tiptitle = isWithinOneHour ? '行程即将开始，是否确认取消行程。' : '是否确认取消行程'

    } else {
      Tiptitle = isWithinOneHour ? '行程即将开始，是否确认取消订单。' : '是否确认取消订单'
    }

    wx.showModal({
      title: '提示',
      content: Tiptitle,
      success(res) {
        if (res.confirm) {
          that.postcancelPassenger()
        }
      }
    })
  },
  // 乘客行程确认/到达
  TravelConfirmation() {
    this.getLocation()
    let that = this
    let item = this.data.TravelInformation
    wx.showModal({
      title: '提示',
      content: item.details_by == 1 ? '行程确认上车' : '行程确认到达',
      success(res) {
        if (res.confirm) {
          wx.showLoading()
          let data = {
            id: item.id,
            phone: wx.getStorageSync('TwoPlusTravel').phone,
            latitude: app.globalData.position.latitude,
            longitude: app.globalData.position.longitude
          }
          if (item.details_by == 1) {
            that.PIyConfirmation(data)
          } else {
            that.PassengersArriveAtTrip(data)
          }
        }
      }
    })

  },
  // 乘客行程确认
  PIyConfirmation(data) {
    let that = this
    util.request('POST', api.postcarConfirm, data).then(function (res) {
      wx.hideLoading()
      console.log(res);
      if (res.code == 200) {
        wx.showToast({
          title: '确认成功！',
        })
        that.ResettheAcquisition()
      } else {
        wx.showToast({
          title: '行程异常~',
          icon: 'error'
        })
      }
    })
  },
  // 乘客行程到达
  PassengersArriveAtTrip(data) {
    let that = this
    util.request('POST', api.TheTripCancelArrive, data).then(function (res) {
      wx.hideLoading()
      switch (res.code) {
        case 100:
          wx.showToast({
            title: '数据异常！',
            icon: 'error'
          })
          that.ResettheAcquisition()
          break;
        case 101:
          wx.showToast({
            title: '行程未出发，无法点击到达',
            icon: 'none'
          })
          that.ResettheAcquisition()
          break;
        case 200:
          wx.showToast({
            title: '确认成功 行程结束',
            icon: 'none'
          })
          let timer = setTimeout(() => {
            that.MyScheduleOrder()
            clearTimeout(timer)
          }, 1000)
          break;
        case 300:
          wx.showToast({
            title: '订单数据异常',
            icon: 'none'
          })
          that.ResettheAcquisition()
          break;
        case 301:
          wx.showToast({
            title: '请先确定上车',
            icon: 'none'
          })
          that.ResettheAcquisition()
          break;
        default:
          wx.showToast({
            title: '行程异常~',
            icon: 'error'
          })
          that.ResettheAcquisition()
          break;
      }
    })
  },
  // 司机行程弹窗确认 出发/到达
  driverOut(event) {
    const status = event.currentTarget.dataset.status;
    this.getLocation()
    let that = this
    let item = this.data.TravelInformation
    if (item.update_by === 0) return
    if (item.update_by === 4) return

    const content = status == 1 ? '行程确认出发?' : "行程确认结束?"

    wx.showModal({
      title: '提示',
      content,
      success(res) {
        if (res.confirm) {
          let data = {
            id: item.id,
            phone: wx.getStorageSync('TwoPlusTravel').phone,
            latitude: app.globalData.position.latitude,
            longitude: app.globalData.position.longitude
          }
          wx.showLoading()
          status == 1 ? that.TheTripSetOut(data) : that.TheTripCarArrive(data)
        }
      }
    })
  },
  // 司机出发
  TheTripSetOut(data) {
    let that = this
    util.request('POST', api.TheTripSetOut, data).then(function (res) {
      wx.hideLoading()
      console.log(res);
      if (res.code == 200) {
        wx.showToast({
          title: '行程已确认出发',
          icon: 'none'
        })
        that.ResettheAcquisition()
      } else {
        wx.showToast({
          title: '数据异常',
          icon: 'none'
        })
      }
    })
  },
  // 司机到达
  TheTripCarArrive(data) {
    let that = this
    util.request('POST', api.TheTripCarArrive, data).then(function (res) {
      wx.hideLoading()
      if (res.code == 100) {
        wx.showToast({
          title: '行程异常',
          icon: 'none'
        })
      } else if (res.code == 200) {
        wx.showToast({
          title: '已到达目的地 行程已结束',
          icon: 'none'
        })
        let timer = setTimeout(() => {
          that.MyScheduleOrder()
          clearTimeout(timer)
        }, 1000)
        return
      } else if (res.code == 300) {
        wx.showToast({
          title: '离目的地距离太远，无法结束行程',
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '数据写入异常，请联系管理员',
          icon: 'none'
        })
      }
      that.ResettheAcquisition()
    })
  },
  // 行程取消
  postcancelPassenger() {
    let that = this
    let item = this.data.TravelInformation
    let role = this.data.role
    // if (role == 1) {
    //   if (item.update_by != 1) {
    //     wx.showToast({
    //       title: '行程已出发 不可中途取消',
    //       icon: 'none'
    //     })
    //     return
    //   }
    // }else {
    //   if (item.details_by != 1) {
    //     wx.showToast({
    //       title: '该行程正在进行中~',
    //       icon: 'none'
    //     })
    //     return
    //   }
    // }
    let data = {
      id: this.data.role === 1 ? item.id : (this.data.role === 2 ? item.details_id : null),
      phone: wx.getStorageSync('TwoPlusTravel').phone
    }
 
//     console.log(this.data.role)
// console.log(data)
//     return
    util.request('POST', role == 2 ? api.postcancelPassenger : api.postdrivercancelPassenger, data).then(function (res) {
      if (role == 1) {
        if (res.code == 200 || res.code == 400) {
          wx.showToast({
            title: '取消成功',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '行程异常~',
            icon: 'error'
          })
        }
      } else {
        if (res.code == 200 || res.code == 400) {
          wx.showToast({
            title: '取消成功 退款处理中',
            icon: 'none'
          })
        } else if (res.code == 300) {
          wx.showToast({
            title: '退款状态异常',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '行程异常~',
            icon: 'error'
          })
        }
      }
      // clearInterval(that.data.intervalId)
      // that.setData({
      //   intervalId: null
      // })
      let timer = setTimeout(() => {
        wx.reLaunch({
          url: '/pages/index/index'
        })
        clearTimeout(timer)
      }, 1000)
    })
  },
  // 调起手机拨号
  dialANumber(e) {
    let num = e.currentTarget.dataset.num
    wx.makePhoneCall({
      phoneNumber: num
    })
  },
  gotoAddress(e) {
    let index = e.currentTarget.dataset.index
    let TravelInformation = this.data.TravelInformation
    console.log(TravelInformation);
    let latitude = '0'
    let longitude = "0"
    let address = ''
    if (index == '0') {
      latitude = TravelInformation.platitude
      longitude = TravelInformation.plongitude
      address = TravelInformation.setAddress
    } else {
      latitude = TravelInformation.alatitude
      longitude = TravelInformation.alongitude
      address = TravelInformation.reachAddress
    }
    wx.openLocation({
      latitude: Number(latitude),
      longitude: Number(longitude),
      scale: 16,
      name: address,
      // address: address,
      success(data) {
        console.log(data);
      },
      fail(err) {
        console.log(err);
      }
    })
  },
  cancelBtns(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    if (index.update_by != 1) {
      return
    }
    const lastFourDigits = index.phone.match(/\d{4}$/);
    wx.showModal({
      title: '提示',
      content: `是否确认取消尾号${lastFourDigits}乘客的订单 ?`,
      success(res) {
        if (res.confirm) {
          wx.showLoading()
          that.carCancelPassenger(index.id, that.data.TravelInformation.phone)
        }
      }
    })
  },
  carCancelPassenger(id, phone) {
    let that = this
    let data = {
      id: id,
      phone: phone
    }
    util.request('POST', api.carCancelPassenger, data).then(function (res) {
      wx.hideLoading()
      if (res.code == 200) {
        wx.showToast({
          title: '取消成功',
        })
      } else {
        wx.showToast({
          title: '取消失败',
          icon: 'error'
        })
      }
      that.ResettheAcquisition()
    })
  },
  // 图片轮播图隐藏
  PicturesShowUp() {
    this.setData({
      PicturesShow: false
    })
  },
  // 当前图片index
  swiperNumchange(e) {
    let answerSlideshow = this.data.answerSlideshow
    let current = e.detail.current
    let swiperNum = this.data.swiperNum
    console.log(answerSlideshow[swiperNum]);
    if (answerSlideshow[swiperNum].category == 2) {
      const videoContext = wx.createVideoContext(`video${swiperNum}`); // 'myVideo'是视频组件的id
      videoContext.pause();
    }
    this.setData({
      swiperNum: current
    })
  },
  // 修改滑动处理
  swiperChange(e) {
    const current = e.detail.current;
    this.setData({ 
      swiperNum: current 
    });
    
    // 当滑动到最后一页时触发生成新海报
    if (current === this.data.answerSlideshow.pages.length - 1) {
      this.PosterGeneration();
    }
  },

 // 修改后的海报生成方法
PosterGeneration() {
    let that = this;
    wx.showLoading({ title: '生成中...' });
    
    const newBackgroundId = this.data.answerSlideshow.currentId 
      ? this.data.answerSlideshow.currentId + 1 
      : 1;
  
    util.request('POST', api.getPoster, {
      id: this.data.id,
      isShare: "1",
      background_id: newBackgroundId
    }).then(function(res) {
      // 新增业务状态码检查
      
      
      // 解析正确的数据结构
      const newPage = {
        id: newBackgroundId,
        background_url: res.haibao_url,
        category: res.category,
        HasBeenGenerated: true
      };
      console.log(newPage)
      that.setData({
        PicturesShow: true,
        answerSlideshow: {
          pages: [...that.data.answerSlideshow.pages, newPage], // 追加单页数据
          total: res.pages, // 总页数用于分页控制
          currentId: newBackgroundId
        },
        swiperNum: that.data.answerSlideshow.pages.length // 定位到最新页
      });
      
      wx.hideLoading();
    }).catch((error) => {
      wx.hideLoading();
      wx.showToast({ 
        title: error.message || '生成失败', 
        icon: 'none',
        duration: 2000
      });
    });
  },

   // 增强的图片保存方法
   saveThePictures(e) {
    // let item = e.currentTarget.dataset.item;
    let that = this
    let pic = e.currentTarget.dataset.item
    
    // 如果不是图片类型直接阻止操作
    if (pic.category !== 1) return 
  
    wx.showLoading({
      title: pic.HasBeenGenerated ? '正在保存海报' : '正在生成海报'
    })
  
    wx.getSetting({
      success(res) {
        // 权限检查回调
        const handleAuth = () => {
          if (pic.HasBeenGenerated) {
            that.savePhoto(pic.blank_url)
          } else {
            that.GenerateThePoster(pic.id)
          }
        }
  
        // 未授权时
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: handleAuth,
            fail() {
              wx.showModal({
                title: '权限提示',
                content: '需要相册权限保存图片',
                success(modalRes) {
                  if (modalRes.confirm) {
                    wx.openSetting({
                      success(settingRes) {
                        if (settingRes.authSetting['scope.writePhotosAlbum']) {
                          handleAuth()
                        }
                      }
                    })
                  }
                }
              })
            }
          })
        } else {
          // 已授权直接处理
          handleAuth()
        }
      },
      fail(err) {
        wx.showToast({
          title: '权限检查失败',
          icon: 'none'
        })
      }
    })
  },


  // 生成海报
  GenerateThePoster(e) {
    let that = this
    let data = {
      "id": this.data.id, //行程ID
      "isShare": "1",
      "background_id": 1 //背景ID
    }
    
console.log(data)
// return
    try {
      util.request('POST', api.getPoster, data).then(function (res) {
        
        let answerSlideshow = that.data.answerSlideshow
        answerSlideshow.forEach((item) => {
          if (item.id == res.background_id) {
            item.blank_url = res.haibao_url
            item.background_url = res.haibao_url
            item.HasBeenGenerated = true
          }
        })
        that.setData({
          answerSlideshow
        })
        that.savePhoto(res.haibao_url);
        wx.hideLoading()
      })
    } catch (error) {
      wx.hideLoading()
      wx.showToast({
        title: '生成失败',
        icon: 'error'
      })
    }
  },
  // 预览
  saveThePreview(e) {
    let pic = e.currentTarget.dataset.item
    console.log(pic);
    if (pic.category != 1) {
      return
    }
    this.setData({
      previewimg: pic.background_url,
      previewShow: true
    })
  },
  //保存图片到相册，提示保存成功
  savePhoto(imgUrl) {
    let that = this
    console.log(imgUrl);
    wx.downloadFile({
      url: imgUrl,
      success: function (res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: "success",
              duration: 1000
            })
          },
          fail(err) {
            console.log(err);
            wx.showToast({
              title: '保存失败',
              icon: "error",
              duration: 1000
            })
          }
        })
      },
      fail(err) {
        console.log(err);
        wx.showToast({
          title: '保存失败',
          icon: "error",
          duration: 1000
        })
      }
    })
  },
  saveVideo(videoUrl) {
    wx.downloadFile({
      url: videoUrl,
      success: function (res) {
        if (res.statusCode === 200) {
          wx.saveVideoToPhotosAlbum({
            filePath: res.tempFilePath,
            success(res) {
              wx.showToast({
                title: '保存成功',
                icon: 'success'
              });
            },
            fail(err) {
              wx.showToast({
                title: '保存失败',
                icon: 'none'
              });
            }
          });
        }
      },
      fail: function (err) {
        wx.showToast({
          title: '下载失败',
          icon: 'none'
        });
      }
    });
  },
  previewonClose() {
    this.setData({
      previewShow: !this.data.previewShow
    })
  },
  TheSeatFilled() {
    if (this.data.TravelInformation.update_by < 3) {
      this.setData({
        show: !this.data.show
      })
    }
  },
  PreSelectedSeats(e) {
    let seat = e.currentTarget.dataset.index + 1
    this.setData({
      PreSeat: seat
    })
  },
  onConfirm() {
    let that = this
    let PreSeat = this.data.PreSeat
    let data = {
      id: this.data.id,
      seat: PreSeat
    }
    util.request('POST', api.postSeatEsc, data).then(function (res) {
      if (res.code == 200) {
        wx.showToast({
          title: '修改成功！',
        })
        that.ResettheAcquisition()
      } else {
        wx.showToast({
          title: '修改失败 请稍后再试！',
          icon: 'none'
        })
      }
      that.setData({
        show: false
      })
    })
  },
  // 驳回
  bargainingReject(e) {
    let item = e.currentTarget.dataset.index
    let data = {
      id: item.id,
      phone: wx.getStorageSync('TwoPlusTravel').phone
    }
    let lastFourDigits = item.phone.slice(-4)
    let that = this
    wx.showModal({
      title: '提示',
      content: `驳回尾号${lastFourDigits}议价申请`,

      success(res) {
        if (res.confirm) {
          util.request('POST', api.postBargainingReject, data).then(function (res) {
            if (res.code == 200) {
              wx.showToast({
                title: `尾号${lastFourDigits}的议价已驳回`,
                icon: 'none'
              })
              that.ResettheAcquisition()
            } else {
              wx.showToast({
                title: '操作失败 请稍后再试！',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },
  // 同意
  bargainingAgree(e) {
    let item = e.currentTarget.dataset.index
    let data = {
      id: item.id,
      phone: wx.getStorageSync('TwoPlusTravel').phone
    }
    let lastFourDigits = item.phone.slice(-4)
    let that = this
    wx.showModal({
      title: '提示',
      content: `同意尾号${lastFourDigits}的议价申请`,
      success(res) {
        if (res.confirm) {
          util.request('POST', api.postCarBargainingAgree, data).then(function (res) {
            if (res.code == 200) {
              wx.showToast({
                title: `尾号${lastFourDigits}议价已同意 请联系乘客进行支付`,
                icon: 'none',
                duration: 2000
              })
              that.ResettheAcquisition()
            } else {
              wx.showToast({
                title: '操作失败 请稍后再试！',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },
  ResettheAcquisition() {
    this.getDetailsTrip()
    clearInterval(this.data.intervalId)
    this.setData({
      intervalId: setInterval(() => {
        this.getCarDetailsTriAjax()
      }, 10000)
    })
  },

  ShareCardShowClose() {
    this.setData({
      ShareCardShow: false
    })
  },
  InTheSharing() {
    this.setData({
      Sharing: true
    })
  }
})